package spotGUI.ElementViewer;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import managers.DBManager;
import managers.socialNetworkManagers.ElementsManager;
import managers.socialNetworkManagers.SocialNetworkManager;
import net.tomahawk.ExtensionsFilter;
import net.tomahawk.XFileDialog;
import spotGUI.GUIElements.MarkerGUI.StarMarkerPanel;
import utils.ImageUtils;
import utils.MaxLenghtTextDocument;
import utils.StackTracePrinter;
import elements.Element;
import java.util.ResourceBundle;

public class AddElementPanel extends JPanel {

	private static final int MAXIMUM_PHOTO_WIDTH = 190;
	private static final int SHOW_STATUS_DELAY = 5000;
	
	private JLabel lblNuovoPost;
	private JLabel lblNome;
	private JTextField textFieldPostName;
	private JLabel lblContenuto;
	private JTextArea textAreaPost;
	private JLabel lblPhoto;
	private JButton btnFoto;
	private JButton btnAggiungi;
	private JScrollPane scrollPane;
	private JTextArea textAreaComment;
	private JScrollPane scrollPane_1;
	private JButton btnAggiungiCommento;
	private JLabel label;
	private JButton btnAggiungiVoto;
	private JLabel lblStatus;
	private JLabel lblPostSelezionato;

	private Font font = new Font("Agency FB", Font.BOLD, 23);
	private StarMarkerPanel marker;
	private JLabel label_1;

	private Element currentlySelectedElement = null;
	private ImageIcon currentlySelectedPhoto = null;
	private XFileDialog fileChooser;
	
	private Timer t;

	private ActionListener selectPhotoListener = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			String fileName = fileChooser.getFile();
			String directory = fileChooser.getDirectory();
			if (fileName != null) {
				currentlySelectedPhoto = resizePhoto(fileName, directory);
				lblPhoto.setText(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("AddElementPanel.1"));
			}
		}
	};
	private Box horizontalBox;
	private JButton btnReset;
	private Component horizontalStrut;



	public AddElementPanel(final ElementPanel elementPanel) {
		setBorder(new EmptyBorder(10, 10, 10, 10));


		t = new Timer(SHOW_STATUS_DELAY, new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				setStatus("");
				
			}
		});
		
		fileChooser = new XFileDialog("Sfoglia");
		Vector<String> extensions = new Vector<String>();
		extensions.addElement("png");
		extensions.addElement("jpg");
		extensions.addElement("bmp");
		fileChooser.addFilters(new ExtensionsFilter("File immagine", extensions));

		setPreferredSize(new Dimension(664, 250));
		setMinimumSize(new Dimension(664, 250));
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{61, 268, 40, 116, 7};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, 1.0, 0.0};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 1.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);

		lblNuovoPost = new JLabel(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("AddElementPanel.lblNuovoPost.text")); //$NON-NLS-1$ //$NON-NLS-2$
		lblNuovoPost.setFont(font);
		GridBagConstraints gbc_lblNuovoPost = new GridBagConstraints();
		gbc_lblNuovoPost.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblNuovoPost.insets = new Insets(0, 0, 5, 5);
		gbc_lblNuovoPost.gridx = 1;
		gbc_lblNuovoPost.gridy = 0;
		add(lblNuovoPost, gbc_lblNuovoPost);

		lblPostSelezionato = new JLabel(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("AddElementPanel.lblPostSelezionato.text")); //$NON-NLS-1$ //$NON-NLS-2$
		lblPostSelezionato.setHorizontalAlignment(SwingConstants.RIGHT);
		GridBagConstraints gbc_lblPostSelezionato = new GridBagConstraints();
		gbc_lblPostSelezionato.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblPostSelezionato.gridwidth = 2;
		gbc_lblPostSelezionato.insets = new Insets(0, 0, 5, 0);
		gbc_lblPostSelezionato.gridx = 3;
		gbc_lblPostSelezionato.gridy = 0;
		add(lblPostSelezionato, gbc_lblPostSelezionato);

		lblNome = new JLabel(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("AddElementPanel.lblNome.text")); //$NON-NLS-1$ //$NON-NLS-2$
		lblNome.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNome.setHorizontalTextPosition(SwingConstants.LEFT);
		GridBagConstraints gbc_lblNome = new GridBagConstraints();
		gbc_lblNome.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblNome.insets = new Insets(0, 0, 5, 5);
		gbc_lblNome.gridx = 0;
		gbc_lblNome.gridy = 1;
		add(lblNome, gbc_lblNome);

		textFieldPostName = new JTextField();
		textFieldPostName.setDocument(new MaxLenghtTextDocument(50));
		textFieldPostName.setBackground(UIManager.getColor("Desktop.background"));
		GridBagConstraints gbc_textFieldPostName = new GridBagConstraints();
		gbc_textFieldPostName.insets = new Insets(0, 0, 5, 5);
		gbc_textFieldPostName.fill = GridBagConstraints.HORIZONTAL;
		gbc_textFieldPostName.gridx = 1;
		gbc_textFieldPostName.gridy = 1;
		add(textFieldPostName, gbc_textFieldPostName);
		textFieldPostName.setColumns(10);

		label = new JLabel(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("AddElementPanel.label.text")); //$NON-NLS-1$ //$NON-NLS-2$
		label.setFont(font);
		GridBagConstraints gbc_label = new GridBagConstraints();
		gbc_label.fill = GridBagConstraints.HORIZONTAL;
		gbc_label.insets = new Insets(0, 0, 5, 5);
		gbc_label.gridx = 3;
		gbc_label.gridy = 1;
		add(label, gbc_label);

		lblContenuto = new JLabel(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("AddElementPanel.lblContenuto.text")); //$NON-NLS-1$ //$NON-NLS-2$
		lblContenuto.setHorizontalAlignment(SwingConstants.RIGHT);
		GridBagConstraints gbc_lblContenuto = new GridBagConstraints();
		gbc_lblContenuto.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblContenuto.insets = new Insets(0, 0, 5, 5);
		gbc_lblContenuto.gridx = 0;
		gbc_lblContenuto.gridy = 2;
		add(lblContenuto, gbc_lblContenuto);

		scrollPane = new JScrollPane();
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.insets = new Insets(0, 0, 5, 5);
		gbc_scrollPane.gridx = 1;
		gbc_scrollPane.gridy = 2;
		add(scrollPane, gbc_scrollPane);

		textAreaPost = new JTextArea();
		textAreaPost.setDocument(new MaxLenghtTextDocument(5000));
		textAreaPost.setWrapStyleWord(true);
		textAreaPost.setLineWrap(true);
		textAreaPost.setOpaque(false);
		scrollPane.setViewportView(textAreaPost);

		scrollPane_1 = new JScrollPane();
		scrollPane_1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
		gbc_scrollPane_1.gridwidth = 2;
		gbc_scrollPane_1.fill = GridBagConstraints.BOTH;
		gbc_scrollPane_1.insets = new Insets(0, 0, 5, 0);
		gbc_scrollPane_1.gridx = 3;
		gbc_scrollPane_1.gridy = 2;
		add(scrollPane_1, gbc_scrollPane_1);

		textAreaComment = new JTextArea();
		textAreaComment.setDocument(new MaxLenghtTextDocument(5000));
		textAreaComment.setLineWrap(true);
		textAreaComment.setWrapStyleWord(true);
		textAreaComment.setOpaque(false);
		scrollPane_1.setViewportView(textAreaComment);

		btnFoto = new JButton(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("AddElementPanel.btnFoto.text")); //$NON-NLS-1$ //$NON-NLS-2$
		btnFoto.addActionListener(selectPhotoListener);
		GridBagConstraints gbc_btnFoto = new GridBagConstraints();
		gbc_btnFoto.insets = new Insets(0, 0, 5, 5);
		gbc_btnFoto.gridx = 0;
		gbc_btnFoto.gridy = 3;
		add(btnFoto, gbc_btnFoto);

		lblPhoto = new JLabel(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("AddElementPanel.lblPhoto.text")); //$NON-NLS-1$ //$NON-NLS-2$
		GridBagConstraints gbc_lblPhoto = new GridBagConstraints();
		gbc_lblPhoto.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblPhoto.insets = new Insets(0, 0, 5, 5);
		gbc_lblPhoto.gridx = 1;
		gbc_lblPhoto.gridy = 3;
		add(lblPhoto, gbc_lblPhoto);

		btnAggiungiCommento = new JButton(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("AddElementPanel.btnAggiungiCommento.text")); //$NON-NLS-1$ //$NON-NLS-2$
		btnAggiungiCommento.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (currentlySelectedElement == null) {
					lblPostSelezionato.setText(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("AddElementPanel.lblPostSelezionato.text"));
					return;
				}
				if (textAreaComment.getText().equals("")) {
					setStatus(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("AddElementPanel.2"));
					return;
				}


				try {
					ElementsManager.comment(DBManager.getConnection(), currentlySelectedElement.elementId,
							SocialNetworkManager.getSharedInstance().getUserId(), textAreaComment.getText());
					setStatus(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("AddElementPanel.3"));
					elementPanel.update();
				} catch (SQLException e) {
					StackTracePrinter.print(e);
					setStatus(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("AddElementPanel.4"));
				} finally {
					resetCommentsAndVotes();
				}
			}
		});
		GridBagConstraints gbc_btnAggiungiCommento = new GridBagConstraints();
		gbc_btnAggiungiCommento.anchor = GridBagConstraints.EAST;
		gbc_btnAggiungiCommento.insets = new Insets(0, 0, 5, 0);
		gbc_btnAggiungiCommento.gridx = 4;
		gbc_btnAggiungiCommento.gridy = 3;
		add(btnAggiungiCommento, gbc_btnAggiungiCommento);

		horizontalBox = Box.createHorizontalBox();
		GridBagConstraints gbc_horizontalBox = new GridBagConstraints();
		gbc_horizontalBox.anchor = GridBagConstraints.EAST;
		gbc_horizontalBox.insets = new Insets(0, 0, 5, 5);
		gbc_horizontalBox.gridx = 1;
		gbc_horizontalBox.gridy = 4;
		add(horizontalBox, gbc_horizontalBox);

		btnReset = new JButton(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("AddElementPanel.btnReset.text")); //$NON-NLS-1$ //$NON-NLS-2$
		btnReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				resetPosts();
			}
		});
		horizontalBox.add(btnReset);

		horizontalStrut = Box.createHorizontalStrut(5);
		horizontalBox.add(horizontalStrut);

		btnAggiungi = new JButton(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("AddElementPanel.btnAggiungi.text")); //$NON-NLS-1$ //$NON-NLS-2$
		horizontalBox.add(btnAggiungi);
		btnAggiungi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (textFieldPostName.getText().equals("") || textAreaPost.getText().equals("")) {
					setStatus("Non hai inserito il nome o il testo del messaggio");
					return;
				}
				Element element = new Element(Element.POST, SocialNetworkManager.getSharedInstance().getUserId(),
						textFieldPostName.getText(), textAreaPost.getText(), currentlySelectedPhoto);
				try {
					ElementsManager.addElement(DBManager.getConnection(), element);
					setStatus(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("AddElementPanel.4"));
					elementPanel.update();
				} catch (SQLException e1) {
					StackTracePrinter.print(e1);
					setStatus(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("AddElementPanel.5"));
				} finally {
					resetPosts();
				}
			}
		});

		label_1 = new JLabel(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("AddElementPanel.label_1.text")); //$NON-NLS-1$ //$NON-NLS-2$
		label_1.setFont(new Font("Agency FB", Font.BOLD, 23));
		GridBagConstraints gbc_label_1 = new GridBagConstraints();
		gbc_label_1.fill = GridBagConstraints.HORIZONTAL;
		gbc_label_1.insets = new Insets(0, 0, 5, 5);
		gbc_label_1.gridx = 3;
		gbc_label_1.gridy = 4;
		add(label_1, gbc_label_1);

		lblStatus = new JLabel("");
		lblStatus.setIcon(new ImageIcon(AddElementPanel.class.getResource("/images/Icons/24x24px/info.png")));
		GridBagConstraints gbc_lblStatus = new GridBagConstraints();
		gbc_lblStatus.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblStatus.gridwidth = 2;
		gbc_lblStatus.insets = new Insets(0, 0, 0, 5);
		gbc_lblStatus.gridx = 0;
		gbc_lblStatus.gridy = 5;
		add(lblStatus, gbc_lblStatus);

		marker = new StarMarkerPanel();
		marker.setOpaque(false);
		GridBagConstraints gbc_marker = new GridBagConstraints();
		gbc_marker.anchor = GridBagConstraints.EAST;
		gbc_marker.insets = new Insets(0, 0, 0, 5);
		gbc_marker.gridx = 3;
		gbc_marker.gridy = 5;
		add(marker, gbc_marker);

		btnAggiungiVoto = new JButton(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("AddElementPanel.btnAggiungiVoto.text")); //$NON-NLS-1$ //$NON-NLS-2$
		btnAggiungiVoto.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (currentlySelectedElement == null) {
					lblPostSelezionato.setText(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("AddElementPanel.lblPostSelezionato.text"));
					return;
				}
				if (marker.getMark() == 0) {
					setStatus(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("AddElementPanel.6"));
					return;
				}

				try {
					ElementsManager.vote(DBManager.getConnection(), currentlySelectedElement.elementId,
							SocialNetworkManager.getSharedInstance().getUserId(), marker.getMark());
					setStatus(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("AddElementPanel.7"));
					elementPanel.update();
				} catch (SQLException e1) {
					if (e1.getErrorCode() == 1062)
						setStatus(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("AddElementPanel.8"));
					else {
						setStatus(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("AddElementPanel.9"));
						StackTracePrinter.print(e1);
					}
				} finally {
					resetCommentsAndVotes();
				}
			}
		});
		GridBagConstraints gbc_btnAggiungiVoto = new GridBagConstraints();
		gbc_btnAggiungiVoto.anchor = GridBagConstraints.WEST;
		gbc_btnAggiungiVoto.gridx = 4;
		gbc_btnAggiungiVoto.gridy = 5;
		add(btnAggiungiVoto, gbc_btnAggiungiVoto);

	}

	public void setSelectedElement (Element e) {
		this.currentlySelectedElement = e;
		lblPostSelezionato.setText(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("AddElementPanel.10")+e.name);
	}

	private void resetCommentsAndVotes () {
		lblPostSelezionato.setText(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("AddElementPanel.lblPostSelezionato.text"));
		textAreaComment.setText("");
		currentlySelectedElement = null;
	}

	private void resetPosts () {
		textAreaPost.setText("");
		textFieldPostName.setText("");
		currentlySelectedPhoto = null;
		lblPhoto.setText(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("AddElementPanel.lblPhoto.text"));
	}

	private ImageIcon resizePhoto(String fileName, String directory) {
		currentlySelectedPhoto = new ImageIcon(directory+fileName);
		int w, h;
		if (currentlySelectedPhoto.getIconWidth() > currentlySelectedPhoto.getIconWidth()) {
			w=Math.min(MAXIMUM_PHOTO_WIDTH, currentlySelectedPhoto.getIconWidth());
			h=-1;
		} else {
			w=-1;
			h=Math.min(MAXIMUM_PHOTO_WIDTH, currentlySelectedPhoto.getIconHeight());
		}
		return ImageUtils.resize(currentlySelectedPhoto, w, h);
	}
	
	private void setStatus (String status) {
		lblStatus.setText(status);
		t.restart();
	}

}
